# Create your views here.

from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from rest_framework import authentication, permissions
from polls.models import Question
from polls.serializer import QuestionSerializers
from django.http import JsonResponse, FileResponse
from rest_framework.decorators import action


class Project(ModelViewSet):
    """
    View to list all users in the system.

    * Requires token authentication.
    * Only admin users are able to access this view.
    """

    # authentication_classes = [authentication.TokenAuthentication]
    # permission_classes = [permissions.IsAdminUser]
    # queryset = Question.objects.filter(id=2)
    serializer_class = QuestionSerializers

    def get_queryset(self):
        print("ppp")
        return Question.objects.all()

    def destroy(self, request, *args, **kwargs):
        return Response({"v": 1})

    def update(self, request, *args, **kwargs):
        return Response({"b": 1})

    def create(self, request, *args, **kwargs):
        return Response({"a": 1})

    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        serializer = self.get_serializer(instance)
        return Response(serializer.data)

    def list(self, request, *args, **kwargs):
        """
        Return a list of all users.
        """
        usernames = self.get_queryset()
        seri = self.get_serializer(usernames, many=True)
        print(seri.data)
        return Response(seri.data, status=200)


class Project2(GenericAPIView):
    """
    View to list all users in the system.

    * Requires token authentication.
    * Only admin users are able to access this view.
    """

    # authentication_classes = [authentication.TokenAuthentication]
    # permission_classes = [permissions.IsAdminUser]
    queryset = Question.objects.all()
    serializer_class = QuestionSerializers

    def get(self, request):
        usernames = self.get_queryset()
        seri = self.get_serializer(usernames, many=True)
        return JsonResponse(seri.data, status=200, safe=False)

    def post(self, request):
        print(request.data, "post")
        return JsonResponse({"q": 1})

    def delete(self, request):
        print(request.data, "delete")
        return JsonResponse({"q": 1})

    def put(self, request):
        print(request.data, "put")
        return JsonResponse({"q": 1})

    def patch(self, reuqest):
        print(reuqest.data, "patch")
        return JsonResponse({"q": 1})


from rest_framework.decorators import api_view
from rest_framework.reverse import reverse


@api_view(['GET'])
def api_root(request, format=None):
    return JsonResponse({
        'users': reverse('user-list', request=request, format=format),
        'snippets': reverse('snippet-list', request=request, format=format)
    })


def testee(request):
    from polls.service.test_queryset import select
    ab = select()
    print(ab)
    return JsonResponse({'a': 1})
